<template>
  <el-card class="login-card">
    <h2>用户登录</h2>
    <el-form :model="formData">
      <el-form-item label="用户名">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="formData.password" type="password" />
      </el-form-item>
      <el-button type="primary" @click="login">登录</el-button>
    </el-form>
  </el-card>
</template>

<script setup>
import { ref } from "vue";
import { useAuthStore } from "@/store/authStore";
import http from "@/api/http";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const formData = ref({ username: "", password: "" });
const authStore = useAuthStore();
const router = useRouter();

const login = async () => {
  try {
    const response = await http.post("/auth/login", formData.value);
    authStore.setToken(response.data.token);
    ElMessage.success("登录成功");
    router.push("/home/file-manager");
  } catch (error) {
    ElMessage.error("登录失败");
  }
};
</script>
